﻿///<reference path="../../js/jquery/jquery.js"/>
/*

File Upload HTTP module for ASP.Net (v 2.0)
Copyright (C) 2007-2008 Darren Johnstone (http://darrenjohnstone.net)

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

*/
/*----------------------------------------------------------------------------------------------
Revision08/08/2008
Using jquery1.2.x,remove prototype.js
-----------------------------------------------------------------------------------------------*/
var up_xhttp;
var up_key;
var up_loading = false;
var up_imagePath;
var up_showCancel;
var up_extensions;

function up_AddUpload(id) {
	$("div.upFileInputs:hidden","#" + id).eq(0).show();
}

function up_ValidateUpload(source, clientside_arguments) {
	var els = $(":file", $(source).parentNode);
	var ok = false;
	els.each(function(i) {
		if (this.value && this.value != "") {
			ok = true;
			return ok;
		}
	});

	clientside_arguments.IsValid = ok;
}

function up_RemoveUpload(e) {
	var container = e.parentNode;
	$(":text", $(container)).val("");
	$(":file", $(container)).each(function(i) {
		$(this).replaceWith('<input type="file" id="'+this.id+'" name="'+this.name+'" class="'+this.className+'"/>');
	});
	$(container).hide();
}

function up_BeginUpload(key, showcancel) {
	if (!Page_IsValid) return;

	up_key = $("#"+key).val();
	up_showCancel = showcancel;
	up_SetProgress(0, '');
}

function up_SetProgress(progress, file) {
	var html;

	html = "<div class='upContainer'>";
	html += "<div class='upOuterBar'>";
	html += "<div id='upProgressBar' class='upInnerBar' style='width:" + progress + "%'>";
	html += "</div>";
	html += "<div id='upLabel' class='upLabel'>";
	html += (progress == 0 ? "Waiting for uploads to start" : "Now uploading " + file + " " + progress + "%");
	html += "</div>";
	html += "</div>";
	html += "</div>";

	// Cancel button
	if (up_showCancel) {
		html += "<img src='" + up_imagePath + "cancelbutton.gif' onclick='location.reload(true)' style='cursor:hand;margin-top:5px' />";
	}

	if (!up_loading) {
		$("#upProgressWrapper").hide();
	}
	else {
		$("#upProgressWrapper").html(html).show();
	}

	up_loading = true;

	if (progress != 100) window.setTimeout('up_ReportProgress()', 1500);
}

function up_ReportProgress() {
	$.ajax({
		dataType: "xml",
		cache:false,
		data: { DJUploadStatus: up_key },
		url: '../../UploadProgress.ashx',
		success: function(res) {
			if (res.documentElement.getAttribute('empty') == 'true') {
				up_SetProgress(0, '');
			}
			else {
				var progress = parseInt(res.documentElement.getAttribute('progress'));
				var file = res.documentElement.getAttribute('file');
				up_SetProgress(progress, file);
				up_awaitingResult = false;
			}
		},
		error: function(res) {
			up_SetProgress(0, '');
			up_awaitingResult = false;
		}
	});
}

/* ======================================================================
The following function for styling input controls comes from
http://www.quirksmode.org/dom/inputfile.html
========================================================================= */

function up_initFileUploads(imagepath, fe) {
	up_imagePath = imagepath;
	up_extensions = fe.split(',');

	var W3CDOM = (document.createElement && document.getElementsByTagName);
	if (!W3CDOM) return;
	var imgPath=imagepath + 'selectbutton.gif';
	var fakeFileUpload = '<div class="upFakeFile"><input class="upFileBox"/><img alt="" src="' + imgPath + '" style="cursor:hand;margin-left:5px;"/></div>';
	$(":file", "div.upFileInputs").after(fakeFileUpload).change(_onFileChange).mouseout(_onFileChange);
} //endof up_initFileUploads

function _onFileChange() {
	var ok = true;

	if (up_extensions.length > 0 && up_extensions[0] != '' && this.value != null && this.value != '') {
		ok = false;
		for (var e = 0; e < up_extensions.length; e++) {
			if (this.value.toLowerCase().lastIndexOf(up_extensions[e]) != -1) {
				ok = true;
				break;
			}
		}
	}

	if (ok) {
		var val = this.value.substr(this.value.lastIndexOf('\\') + 1, this.value.length);
		$(this).siblings("div.upFakeFile").children("input.upFileBox").val(val);
	}
	else {
		alert('Uploads of this type are now allowed.');
		$(this).siblings("div.upFakeFile").children("input.upFileBox").val("");
		$(this).replaceWith('<input type="file" id="' + this.id + '" name="' + this.name + '" class="' + this.className + '"/>');
	}
}


